#include "HitableList.h"
namespace RayTracer
{
	bool HitableList::Hit(const Ray& r, float t_min, float t_max, HitRecord& rec) const {
		HitRecord temp_rec;
		bool hit_anything = false;
		auto closest_so_far = t_max;

		for (auto& object : objects) {
			if (object->Hit(r, t_min, closest_so_far, temp_rec)) {
				hit_anything = true;
				closest_so_far = temp_rec.t;
				rec = temp_rec;
			}
		}

		return hit_anything;
	}

	bool HitableList::BoundingBox(AABB & output_box) const
	{
		if (objects.empty()) return false;

		AABB temp_box;
		bool first_box = true;

		for (const auto& object : objects) 
		{
			if (!object->BoundingBox(temp_box)) return false;
			output_box = first_box ? temp_box : AABB::SurroundingBox(output_box, temp_box);
			first_box = false;
		}

		return true;
	}
}